package com.embracesource.monitor.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.embracesource.monitor.entity.Database;
import com.embracesource.monitor.service.IDatabaseService;
import com.gnerv.pabu.basic.core.entity.Result;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * <p>
 * 基础平台-服务器表 前端控制器
 * </p>
 *
 * @author Gnerv LiGen
 * @since 2019-09-03
 */
@RestController
@RequestMapping("/api/monitor/database")
public class DatabaseController {

    @Resource
    private IDatabaseService databaseService;

    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public Result save(@RequestBody Database database) {
        databaseService.saveDatabase(database);
        return Result.response();
    }

    @RequestMapping(value = "/create", method = RequestMethod.POST)
    public Result create(@RequestBody Database database) {
        databaseService.createDatabase(database);
        return Result.response();
    }

    @RequestMapping(value = "/delete/{databaseId}", method = RequestMethod.GET)
    public Result delete(@PathVariable(value = "databaseId") String databaseId) {
        databaseService.deleteDatabase(databaseId);
        return Result.response();
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public Result update(@RequestBody Database database) {
        databaseService.updateDatabase(database);
        return Result.response();
    }

    @RequestMapping(value = "/select/{databaseId}", method = RequestMethod.GET)
    public Result select(@PathVariable(value = "databaseId") String databaseId) {
        Database database = databaseService.selectDatabase(databaseId);
        return Result.response().data("database", database);
    }

    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public Result list(Database database,
                       @RequestParam(value = "current", defaultValue = "1") Long current,
                       @RequestParam(value = "size", defaultValue = "300") Long size){
        Page<Database> databases = databaseService.listDatabase(database, current, size);
        return Result.response().data("databases", databases);
    }
}
